package com.learn.project2.service.wx.footprint;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.*;
import com.learn.project2.mapper.MarketFootPrintMapper;
import com.learn.project2.mapper.MarketGoodsMapper;
import com.learn.project2.model.vo.wx.footprint.FootprintListInnerVO;
import com.learn.project2.model.vo.wx.footprint.FootprintListVO;
import com.learn.project2.service.wx.feedback.FeedbackService2;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhizhuke
 * @version 1.0
 * @description:
 * @date 2022-01-08 17-47
 */
@Service
public class FootprintServiceImpl2 implements FootprintService2 {

    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketFootPrintMapper footPrintMapper;


    /**
     * @description:
     * @param:
     * @return:
     * @author zhizhuke
     * @date: 2022-01-11 10:32
     */
    @Override
    public FootprintListVO list(Integer page, Integer limit) {

        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        Integer uid = user.getId();


        PageHelper.startPage(page, limit);
        //根据uid查询商品id
        MarketFootPrintExample footPrintExample = new MarketFootPrintExample();
        footPrintExample.createCriteria().andUserIdEqualTo(uid).andDeletedEqualTo(false);
        List<MarketFootPrint> footPrints = footPrintMapper.selectByExample(footPrintExample);
        PageInfo<MarketFootPrint> pageInfo = new PageInfo<>(footPrints);
        int pages = pageInfo.getPages();
        int total = ((int) pageInfo.getTotal());

        List<FootprintListInnerVO> listInnerVOS = new ArrayList<>();
        for (MarketFootPrint footPrint : footPrints) {
            MarketGoods goods = goodsMapper.selectByPrimaryKey(footPrint.getGoodsId());
            FootprintListInnerVO innerVO = new FootprintListInnerVO(goods.getBrief(), goods.getPicUrl(), footPrint.getAddTime(),
                    goods.getId(), goods.getName(), footPrint.getId(), goods.getRetailPrice());
            listInnerVOS.add(innerVO);
        }

        return new FootprintListVO(total, pages, limit, page, listInnerVOS);
    }

    /** 
     * @description: 
     * @param:
     * @return:  
     * @author zhizhuke
     * @date: 2022-01-11 16:46
     */ 
    @Override
    public void delete(Integer id) {
        MarketFootPrint footPrint = new MarketFootPrint();
        footPrint.setId(id);
        footPrint.setDeleted(true);

        footPrintMapper.updateByPrimaryKeySelective(footPrint);
        
    }
}
